﻿function view() {
    cppei.install();

    var app = angular.module('viewApp', []);

    ////加载分页栏
    cppei.paginationPlugin.attach2ngApp(app);

    app.controller("viewController", function ($scope, $http) {

        var paginationInst = cppei.paginationPlugin.attach2ngScope($scope, $http, 'cms', 'article', 'getList')
             .success(function (result) {
                 //debugger;
                 // biz logic
                 $scope.items = result.items;
                 angular.forEach($scope.items, function (data, index) {
                     data.isChecked = false;
                 });
             })
            .error(function () {
                cppei.growlError('error');
            });


        paginationInst.refresh();

        //删除
        $scope.onDelete = function (itemId) {
            var idList = "";
            //debugger;
            if (itemId == "all") {
                angular.forEach($scope.items, function (value, key) {
                    // debugger;
                    if (value.isChecked) {
                        idList = idList + value.id + ",";
                    }
                });
            } else {
                idList = itemId;
            }

            if (idList == "") {
                // debugger;
                cppei.alert('提示', '提请至少选择一项应用程序信息！');

            } else {
                cppei.confirm('提示', '是否删除选中应用程序信息？', function (result) {
                    if (result) {
                        $http({
                            method: 'post',
                            url: cppei.api.getUrl('AppAuth', 'Delete'),
                            data: { Ids: idList }
                        }).success(function (data, status) {
                            if (data.isSuccess) {
                                paginationInst.refresh();
                            } else {
                                cppei.growlError(data.message);
                            }


                        }).error(function (data, status) {
                            //$scope.data = data || "Request failed";
                            cppei.growlError(data.message);
                        });
                    }
                });
            }
        }

        //全选
        $scope.allCheck = function (allChk) {
            //$scope.subCheck = allChk;
            //debugger;
            angular.forEach($scope.items, function (value, key) {
                value.isChecked = allChk;
            })
        }

        $scope.true2Block = function (value) {
            if (value) {
                return 'block';
            } else {
                return 'none';
            }
        }

        //禁止与启用操作
        $scope.onIsEnable = function (itemId, sysact) {
            // debugger;
            var info = "是否启用该应用程序信息？";
            if (sysact == "Disable") {
                info = "是否禁用该应用程序信息？";
            }
            cppei.confirm('提示', info, function (result) {
                if (result) {
                    $http({
                        method: 'post',
                        url: cppei.api.getUrl('AppAuth', sysact),
                        data: { Id: itemId }
                    }).success(function (data, status) {
                        //debugger;
                        if (data.isSuccess) {
                            paginationInst.refresh();
                        } else {
                            cppei.growlError(data.message);
                        }

                    }).error(function (data, status) {
                        cppei.growlError(data.message);
                    });
                }
            });
        }

        //弹出创建程序名称dialog
        $scope.onShowCreate = function () {
            //cppei.showDialog('创建应用程序', 'Module/UMP/AuthCreate.html', $scope.onDialogClosecCallback, { width: 300, height: 150 });
            cppei.switchView('cms/article-create.html');
        };

        // 对话框关闭事件
        $scope.onDialogClosecCallback = function (result, data) {
            if (result) {
                paginationInst.refresh();
            } else {
                // do nothing
            }
        };

        $scope.publish = function (itemId) {
            cppei.confirm('提示', '是否发布选中的文章？', function (result) {
                if (result) {
                    $http({
                        method: 'post',
                        url: cppei.api.getUrl('cms', 'article', 'publish'),
                        data: { id: itemId }
                    }).success(function (data, status) {
                        if (data.isSuccess) {
                            paginationInst.refresh();
                            cppei.alert('发布成功!');
                        } else {
                            cppei.growlError(data.message);
                        }
                    }).error(function (data, status) {
                        //$scope.data = data || "Request failed";
                        cppei.growlError(data.message);
                    });
                }
            });
        }

        $scope.disable = function (itemId) {
            cppei.confirm('提示', '是否禁用选中的文章？', function (closeResult) {
                if (closeResult) {
                    $http({
                        method: 'post',
                        url: cppei.api.getUrl('cms', 'article', 'disable'),
                        data: { id: itemId }
                    }).success(function (result, status) {
                        if (result.isSuccess) {
                            paginationInst.refresh();
                            cppei.growl('禁用成功!');
                        } else {
                            cppei.growlError(data.message);
                        }
                    }).error(function (data, status) {
                        //$scope.data = data || "Request failed";
                        cppei.growlError(data.message);
                    });
                }
            });
        }

        $scope.enable = function (itemId) {
            cppei.confirm('提示', '是否启用用选中的文章？', function (closeResult) {
                if (closeResult) {
                    $http({
                        method: 'post',
                        url: cppei.api.getUrl('cms', 'article', 'enable'),
                        data: { id: itemId }
                    }).success(function (result, status) {
                        if (result.isSuccess) {
                            paginationInst.refresh();
                            cppei.growl('启用成功!');
                        } else {
                            cppei.growlError(data.message);
                        }
                    }).error(function (data, status) {
                        //$scope.data = data || "Request failed";
                        cppei.growlError(data.message);
                    });
                }
            });
        }
    });
}
